

import 'package:bike_ride/common/util/debouncer.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

/// 通知封装
class NotificationUtils {

  final FlutterLocalNotificationsPlugin np = FlutterLocalNotificationsPlugin();

  /// 是否初始化了
  var isInit = false;

  /// 初始化
  void init() {
    if (isInit) return;
    isInit = true;

    var android = const AndroidInitializationSettings("@mipmap/ic_launcher");
    const DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );

    np.initialize(InitializationSettings(android: android, iOS: initializationSettingsDarwin));
  }

  void send(String title, String body) {
    // 初始化
    init();

    // 构建描述
    var androidDetails = const AndroidNotificationDetails(
        'id描述',
        '名称描述',
        importance: Importance.max,
        priority: Priority.high
    );
    var iosDetails = const DarwinNotificationDetails(threadIdentifier: 'thread_id');
    var details = NotificationDetails(
        android: androidDetails,
        iOS: iosDetails
    );

    // 显示通知, 第一个参数是id,id如果一致则会覆盖之前的通知
    np.show(DateTime.now().millisecondsSinceEpoch >> 10, title, body, details);
  }


  showDownloadProgressNotification(int progress) {
    final androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'download_channel',
      'Download Progress',
      importance: Importance.low,
      priority: Priority.low,
      onlyAlertOnce: true,
      showProgress: true,
      maxProgress: 100,
      progress: progress,
    );

    final platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
    );


    if(progress>=100){
      np.cancel(0);
    }else{
      SingleShowProgress.call(() {
        np.show(
          0,
          '发现新版本',
          '$progress% 已下载',
          platformChannelSpecifics,
          payload: 'item x',
        );
      });
    }
  }


  cancelDownloadProgressNotification() async{
    await np.cancel(0);
  }


}
var notification = NotificationUtils();
